package 数组.easy;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Lw中
 * @date 2020/9/8 9:17
 */
public class Yi {

    /**
     * 1.两数之和
     */

    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        // 将数组元素作为键，数组下标作为value
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int other = target - nums[i];
            // 两个数不可以是数组中的同一下标
            if (map.containsKey(other) && map.get(other) != i) {
                return new int [] {i, map.get(other)};
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = {3, 2, 4};
        int target = 6;
        int[] ints = twoSum(nums, target);
        System.out.println(Arrays.toString(ints));
    }

}
